package com.hqyj.javaSpringBoot.modules.account.controller;

import com.github.pagehelper.PageInfo;
import com.hqyj.javaSpringBoot.modules.account.entity.User;
import com.hqyj.javaSpringBoot.modules.account.entity.UserSearchBean;
import com.hqyj.javaSpringBoot.modules.account.service.UserService;
import com.hqyj.javaSpringBoot.modules.common.entiy.ResultEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 127.0.0.1/api/user
     * 添加个人信息
     *  {"email":"zhangsan@123.con","userName":"张三",
     *  "password":"123","userImage":"/imgages/3.jpg"}
     * @param user 用户
     * @return 结果
     */
    @PostMapping(value = "/user",consumes = "application/json")
    public ResultEntity<User> insertUser(@RequestBody User user){
        return userService.insertUser(user);
    }

    /**
     * 127.0.0.1/api/user/{id}
     * 根据id删除用户
     * @param id 用户id
     * @return  结果
     */
    @DeleteMapping(value = "/user/{id}")
    @RequiresPermissions(value = {"删除"})
    public ResultEntity<User> deleteUser(@PathVariable int id) {
        return userService.deleteUser(id);
    }

    /**
     * 127.0.0.1/api/user
     * {"id":2,"email":"zhangsan@123.con","userName":"张三",
     * "password":"123","userImage":"/imgages/3.jpg"}
     * 修改用户信息
     * @param user
     * @return
     */
    @PutMapping(value = "/user",consumes = "application/json")
    @RequiresPermissions(value = {"修改"})
    public ResultEntity<User> updateUser(@RequestBody User user){
        return userService.updateUser(user);
    }

    /**
     * 127.0.0.1/api/userv2/{oldPassWord}
     * {"id":2,"password":"123"}
     * @param user
     * @return
     */
    @PutMapping(value = "/userv2/{oldPassWord}",consumes = "application/json")
    @RequiresPermissions(value = {"修改"})
    public ResultEntity<User> updateUserPassword(@RequestBody User user,@PathVariable String oldPassWord){
        return userService.updateUserPassword(user,oldPassWord);
    }

    /**
     * 127.0.0.1/api/user/{id}
     * 根据用户id查询用户
     * @param id
     * @return
     */
    @GetMapping(value = "/user/{id}")
    public User getUserByUserId(@PathVariable int id){
        return userService.getUserByUserId(id);
    }

    /**
     * 127.0.0.1/api/users
     * {"currentPage":1,"pageSize":5,"orderBy":"id","direction":"desc","keyWord":"张三"}
     * @param userSearchBean
     * @return
     */
    @PostMapping(value = "/users",consumes = "application/json")
    public PageInfo<User> getUserBySearch(@RequestBody UserSearchBean userSearchBean){
        return userService.getUserBySearch(userSearchBean);
    }

    /**
     * 127.0.0.1/api/login
     * 登录验证,密码在实现类加密
     * @param user
     * @return
     */
    @PostMapping(value = "/login",consumes = "application/json")
    public ResultEntity<User> getUserLogin(@RequestBody User user){
        return userService.getUserLogin(user);
    }

}
